package t2.server;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import t2.server.xbee.XBeePort;

import com.rapplogic.xbee.api.AtCommand;
import com.rapplogic.xbee.api.PacketListener;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeResponse;

public class RunND implements Callable<List<XBeeResponse>> {

    List<XBeeResponse> list = new ArrayList<XBeeResponse>();

    @Override
    public List<XBeeResponse> call() throws Exception {
        XBee xbee = XBeePort.getXBeePort().getXbee();

        /* Prepare a packet listener to process the ND response. */
        PacketListener ND = new PacketListener() {

            @Override
            public void processResponse(XBeeResponse response) {
                list.add(response);
            }
        };

        try {
            xbee.addPacketListener(ND);
            xbee.sendAsynchronous(new AtCommand("ND"));
            Thread.sleep(6000);
            xbee.clearResponseQueue();
            xbee.removePacketListener(ND);
        }
        catch (XBeeException e) {
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        return list;
    }
}
